package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * P091Offer.粉刷房子
 * Project: leetcode
 * Package: PACKAGE_NAME
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2022/6/25 14:49
 */
public class P091OfferIIJEj789 {
    public static void main(String[] args) {
        Solution solution = new P091OfferIIJEj789().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        /**
         * 动态规划
         *
         * @param costs
         * @return
         */
        public int minCost(int[][] costs) {
            int n = costs.length;
            int[] dp = new int[3];
            // 初始花dp
            for (int i = 0; i < 3; i++) {
                dp[i] = costs[0][i];
            }

            //遍历所有房子
            for (int i = 1; i < n; i++) {
                int[] newDp = new int[3];
                for (int j = 0; j < 3; j++) {
                    // 利用取于的规则
                    newDp[j] = Math.min(dp[(j + 1) % 3], dp[(j + 2) % 3]) + costs[i][j];
                }
                dp = newDp;
            }


            return Arrays.stream(dp).min().getAsInt();
        }
    }
}
